﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Index_MyAccount : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            var userId = Membership.GetUser().ProviderUserKey;
            PartyFinderDBDataContext db = new PartyFinderDBDataContext();
            aspnet_User user = db.aspnet_Users.SingleOrDefault(x => x.UserId.Equals(userId));
            txtName.Text = user.Name;
            txtLastname.Text = user.LastName;
            txtTlfNr.Text = user.TelephoneNumber.ToString();
            txtBirthYear.Text = user.BirthYear.ToString();
            var genders = from g in db.Genders
                          select g;
            DDLGenderlist.DataSource = genders;
            DDLGenderlist.DataBind();

            ProfilePhotoPicture.ImageUrl = "~/images/ProfilePhotos/" + user.PictureDir;

        }
        

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var userId = Membership.GetUser().ProviderUserKey;
        PartyFinderDBDataContext db = new PartyFinderDBDataContext();
        aspnet_User user = db.aspnet_Users.SingleOrDefault(x => x.UserId.Equals(userId));
        user.Name = txtName.Text;
        user.LastName = txtLastname.Text;
        user.TelephoneNumber = Convert.ToInt32(txtTlfNr.Text);
        user.BirthYear = Convert.ToInt32(txtBirthYear.Text);
        user.GenderId = Convert.ToInt32(DDLGenderlist.SelectedValue);
        db.SubmitChanges();
        txtSaveSucces.Text = "Ændringerne er gemt";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("ChangePassword.aspx");
    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("HomePage.aspx");
    }

    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUploadControl.HasFile)
        {
            try
            {
                if (FileUploadControl.PostedFile.ContentType == "image/jpeg" || FileUploadControl.PostedFile.ContentType == "image/png")
                {
                    if (FileUploadControl.PostedFile.ContentLength < 202400)
                    {
                        string extension = Path.GetExtension(FileUploadControl.PostedFile.FileName); 
                        string filename = Membership.GetUser().UserName;
                        FileUploadControl.SaveAs(Server.MapPath("~/images/ProfilePhotos/") + filename + extension);
                        StatusLabel.Text = "Upload status: File uploaded!";
                        var userId = Membership.GetUser().ProviderUserKey;
                        PartyFinderDBDataContext db = new PartyFinderDBDataContext();
                        aspnet_User user = db.aspnet_Users.SingleOrDefault(x => x.UserId.Equals(userId));
                        user.PictureDir = filename + extension;
                        db.SubmitChanges();

                    }
                    else
                        StatusLabel.Text = "Upload status: The file has to be less than 100 kb!";
                }
                else
                    StatusLabel.Text = "Upload status: Only JPEG files are accepted!";
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    }

}